package com.oig.sys.auth.dao;

import com.oig.sys.auth.entity.TbRoleResource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
@author wuxl
*/
public interface ITbRoleResourceDao extends JpaRepository<TbRoleResource, Integer> {


	TbRoleResource getByRoleIdAndResourceId(Integer roleId, Integer resourceId);

	@Query("select rr.id from TbRoleResource rr where rr.roleId=:roleId")
	List<Integer> findAllIdByRoleId(@Param("roleId") Integer roleId);

	@Query("select rr.id from TbRoleResource rr where rr.resourceId=:resourceId")
	List<Integer> findAllIdByResourceId(@Param("resourceId") Integer resourceId);

	@Query("select rr.id from TbRoleResource rr where rr.roleId=:roleId and rr.id not in(:idList)")
	List<Integer> findAllDelId(@Param("roleId") Integer roleId, @Param("idList") List<Integer> idList);

	@Modifying
	@Query("delete from TbRoleResource rr where rr.id in(:idList)")
	void deleteByIdList(@Param("idList") List<Integer> idList);

	@Query("select rr.roleId from TbRoleResource rr where rr.resourceId=:resourceId")
	List<Integer> findAllRoleByResourceId(@Param("resourceId") Integer resourceId);

	@Query("select rr.resourceId from TbRoleResource rr where rr.roleId=:roleId")
	List<Integer> findAllResourceIdByRole(@Param("roleId") Integer roleId);

	@Query("select rr.resourceId from TbRoleResource rr " +
			" inner join TbResource r on r.id=rr.resourceId" +
			" where rr.roleId in(:roleIdList) and r.systemId=:systemId and r.status=:status")
	List<Integer> findAllResourceIdByRole(@Param("roleIdList") List<Integer> roleIdList, @Param("systemId") Integer systemId, @Param("status") String status);

}
